package dw.fastfile.business.controller;

import dw.fastfile.business.dto.Dog;
import dw.fastfile.business.service.DogPageService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wanghao
 * @create: 2022-11-30 17:23
 **/
@RequestMapping("hello")
@RestController
public class HelloCtrl {
    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Resource
    private DogPageService dogPageService;

    @GetMapping("test")
    public String test() {
        return "test";
    }


    @GetMapping("test3")
    public void test3() throws IOException, TemplateException {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "【我是名字】");
        List<Dog> list = dogPageService.queryNoPage(null);

        map.put("userList", list);


        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("studentInfo.ftl");
        File file = new File("D:/excel_students.xls");

        FileWriter fw = new FileWriter(file);

        template.process(map, fw);

        fw.flush();

        fw.close();

        File file2 = new File("D:/excel_students.pdf");

        FileWriter fw2 = new FileWriter(file2);
        template = freeMarkerConfigurer.getConfiguration().getTemplate("studentInfo.ftl");
        template.process(map, fw);

        fw2.flush();

        fw2.close();
    }

}
